package com.sky.server.impl;

import cn.hutool.core.bean.BeanUtil;
import com.sky.context.MyThrowLocal;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.server.ShoppingCartService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        // 获取用户id
        Long userId = MyThrowLocal.getCurrentId();
        // 判断新增的是套餐还是菜品
        if (shoppingCartDTO.getDishId() != null) {
            // 查询购物车是否有购买过
            ShoppingCart shoppingCart = shoppingCartMapper.selectByDishId(new ShoppingCart(userId, shoppingCartDTO.getDishId(), shoppingCartDTO.getDishFlavor(), null));
            shoppingCart = shoppingCart == null ? new ShoppingCart() : shoppingCart;
            DishVO dish = dishMapper.getById(shoppingCartDTO.getDishId());
            if (shoppingCart.getNumber() == 0) {
                BeanUtil.copyProperties(dish, shoppingCart);
                shoppingCart.setDishId(shoppingCartDTO.getDishId());
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setUserId(userId);
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                shoppingCartMapper.add(shoppingCart);
                return;
            }
            // 如果有购买过记录只需要修改分数和金额就行
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCart.setAmount(shoppingCart.getAmount().add(dish.getPrice()));
            shoppingCartMapper.update(shoppingCart);
            return;
        }

        // 判断新增的是套餐还是菜品
        if (shoppingCartDTO.getSetmealId() != null) {
            // 查询购物车是否有购买过
            ShoppingCart shoppingCart = shoppingCartMapper.selectByDishId(new ShoppingCart(userId, null, shoppingCartDTO.getDishFlavor(), shoppingCartDTO.getSetmealId()));
            shoppingCart = shoppingCart == null ? new ShoppingCart() : shoppingCart;
            Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
            if (shoppingCart.getNumber() == 0) {
                BeanUtil.copyProperties(setmeal, shoppingCart);
                shoppingCart.setNumber(1);
                shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setUserId(userId);
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                shoppingCartMapper.add(shoppingCart);
                return;
            }
            // 如果有购买过记录只需要修改分数和金额就行
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCart.setAmount(shoppingCart.getAmount().add(setmeal.getPrice()));
            shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
            shoppingCartMapper.update(shoppingCart);
            return;
        }


    }

    @Override
    public List<ShoppingCart> list() {
        Long userId = MyThrowLocal.getCurrentId();
        List<ShoppingCart> list = shoppingCartMapper.list(userId);
        return list;
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        Long userId = MyThrowLocal.getCurrentId();
        // 如果是菜品，就根据用户id、菜品id、口味信息去购物车删除
        if (shoppingCartDTO.getDishId() != null) {
            // 先查询后改数量
            ShoppingCart shoppingCart = shoppingCartMapper.selectByDishId(new ShoppingCart(userId, shoppingCartDTO.getDishId(), shoppingCartDTO.getDishFlavor(), null));
            if (shoppingCart.getNumber() - 1 == 0) {
                // 删除购物车订单
                shoppingCartMapper.deleteByIdAndFlavor(shoppingCart);
            } else {
                // 修改数量为-1
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                BigDecimal price = dishMapper.getByShoppingCartId(shoppingCart.getDishId()).getPrice();
                shoppingCart.setAmount(shoppingCart.getAmount().subtract(price));
                shoppingCartMapper.update(shoppingCart);
                return;
            }
        }

        // 如果是套餐就直接去购物车根据用户id、套餐id删除
        // 如果是菜品，就根据用户id、菜品id、口味信息去购物车删除
        if (shoppingCartDTO.getSetmealId() != null) {
            // 先查询后改数量
            ShoppingCart shoppingCart = shoppingCartMapper.selectByDishId(new ShoppingCart(userId, null, null, shoppingCartDTO.getSetmealId()));
            if (shoppingCart.getNumber() - 1 == 0) {
                // 删除购物车订单
                shoppingCartMapper.deleteByIdAndFlavor(shoppingCart);
            } else {
                // 修改数量为-1
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                BigDecimal price = setmealMapper.getById(shoppingCartDTO.getSetmealId()).getPrice();
                shoppingCart.setAmount(shoppingCart.getAmount().subtract(price));
                shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
                shoppingCartMapper.update(shoppingCart);
                return;
            }
        }
    }

    @Override
    public void delete(Long userId) {
        shoppingCartMapper.deleteAll(userId);
    }

}
